﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight;
using System.Json;
using WorkSpace.UI.Windows;
using WorkSpace.UI.WorkSpaceRef;
using WorkSpace.UI.Controls.Windows;
using System.Windows.Data;

namespace WorkSpace.UI.Controls
{
    public partial class CategoryListPart : UserControl
    {
        private string _account;

        public CategoryListPart()
        {
            InitializeComponent();
        }

        public void LoadDatas(string account)
        {
            _account = account;

            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.GetCategoriesCompleted += new EventHandler<WorkSpaceRef.GetCategoriesCompletedEventArgs>(client_GetCategoriesCompleted);
            client.GetCategoriesAsync();
        }

        void client_GetCategoriesCompleted(object sender, WorkSpaceRef.GetCategoriesCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                Category all = new Category();
                all.Caption="全部";
                all.Id = Guid.Empty;
                e.Result.Data.Insert(0, all);
                _ctlGrid.ItemsSource = e.Result.Data;
            }
        }

        private void LoadLinks(Guid categoryId)
        {
            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.GetPrivateLinksCompleted += new EventHandler<GetPrivateLinksCompletedEventArgs>(client_GetPrivateLinksCompleted);
            client.GetPrivateLinksAsync(categoryId);
        }

        void client_GetPrivateLinksCompleted(object sender, GetPrivateLinksCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                _ctlGridLink.ItemsSource = e.Result.Data;
            }
        }

        private void _ctlGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Category selectedCategory = _ctlGrid.SelectedItem as Category;
            if (selectedCategory != null)
            {
                LoadLinks(selectedCategory.Id);
            }
        }

        #region 创建类别
        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            Category category = new Category();
            category.Id = Guid.NewGuid();
            category.Account = _account;

            CategoryWindow window = new CategoryWindow(category);
            window.Closed += new EventHandler(window_Closed);
            window.Show();
        }

        void window_Closed(object sender, EventArgs e)
        {
            CategoryWindow window = sender as CategoryWindow;
            if (window.DialogResult == true)
            {
                LoadDatas(_account);
            }
        }
        #endregion

        #region 连接管理
        private void _ctlBtnCreateLink_Click(object sender, RoutedEventArgs e)
        {
            Category selectedCategory = _ctlGrid.SelectedItem as Category;
            if (selectedCategory != null && selectedCategory.Id != Guid.Empty)
            {
                Link link = new Link();
                link.Id = Guid.NewGuid();
                link.Account = _account;
                link.CategoryId = selectedCategory.Id;
                LinkWindow linkWindow = new LinkWindow(link, selectedCategory.Caption);
                linkWindow.Closed += new EventHandler(linkWindow_Closed);
                linkWindow.Show();
            }
        }

        void linkWindow_Closed(object sender, EventArgs e)
        {
            LinkWindow window = sender as LinkWindow;
            if (window.DialogResult == true)
            {
                Category selectedCategory = _ctlGrid.SelectedItem as Category;
                if (selectedCategory != null)
                {
                    LoadLinks(selectedCategory.Id);
                }
            }
        }

        #region 启用/禁用

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;
            Link link = btn.DataContext as Link;
            
            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.SetEnabledCompleted += new EventHandler<SetEnabledCompletedEventArgs>(client_SetEnabledCompleted);
            client.SetEnabledAsync(link.Id, link);
        }

        void client_SetEnabledCompleted(object sender, SetEnabledCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                Link link = e.UserState as Link;
                link.IsEnable = true;
            }
        }

        private void ButtonDisable_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;
            Link link = btn.DataContext as Link;

            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.SetDisabledCompleted += new EventHandler<SetDisabledCompletedEventArgs>(client_SetDisabledCompleted);
            client.SetDisabledAsync(link.Id, link);
        }

        void client_SetDisabledCompleted(object sender, SetDisabledCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                Link link = e.UserState as Link;
                link.IsEnable = false;
            }
        }

        #endregion

        #endregion
    }

    public class EnableVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isEnable = (bool)value;
            if (!isEnable)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class DisableVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isEnable = (bool)value;
            if (!isEnable)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

